package com.erplan.studentmgt.desktop.db;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;

import com.erplan.studentmgt.desktop.StudentMgt;

public class Database {

	public static void init(){
	    char[] password = {'a','d','m','i','n'};
	    String url = "jdbc:h2:_db/settings";
	    String user = "admin";

		try {
		    Class.forName("org.h2.Driver");
		    Properties prop = new Properties();
		    prop.setProperty("user", user);
		    prop.put("password", password);

		    StudentMgt.db = DriverManager.getConnection(url, prop);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
		    Arrays.fill(password, '0');
		}
	}

	public static void close(){
		try {
			if(StudentMgt.db != null){
				StudentMgt.db.close();
			}
			else{
				System.out.println("Database connection was NULL, skipping!.");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}